#include <iostream>
#include <stdio.h>
#include <vector>
#include <fstream>
#include <stdlib.h>
#define ffor(i,max) for(int i = 0 ; i< max ; i++)
//#define DEBUG

using namespace std;
#ifdef DEBUG
typedef struct linkNode
{
    int value;
    int count;
    struct linkNode * next;
}linkNode;

string arr[300];
int * hashTable = new int[300000000];
void init_hash()
{
    ffor(i,300000000)
        hashTable[i] = -1;

    cout<<"init finished!"<<endl;
}

unsigned int SDBMHash(string str)
{
    unsigned int hash = 0 ;

    const int length = str.length();
    ffor(i,length)
    {
        hash = str[i] + hash<<6 + hash<<16 - hash;
    }
    return hash&0x7FFFFFFF;
}

void createHash(int * hashTable,string * arr,int lenOfHashTable = 300000000 , int lenOfArr = 300)
{
    cout<<"In func createHash!"<<endl;
    ffor(i,lenOfArr)
    {

        if(SDBMHash(arr[i]) >= lenOfHashTable)
        {
            cout<<"Out of range"<<endl;
            continue;
        }

        else if(hashTable[SDBMHash(arr[i])] == -1)
        {
            hashTable[SDBMHash(arr[i])] = atoi(arr[i].c_str());
            cout<<"in position of hashTable, "<<i<<" has the value: "<<arr[i]<<endl;
        }
        else
            cout<<"collision happened! The value is: "<<arr[i]<<endl;
    }

//    ffor(i,lenOfHashTable)
//        cout<<hashTable[i]<<endl;

}

int main()
{
//	freopen("data.in","r",stdin);
    freopen("output.txt","w",stdout);

	init_hash();
	string str;
	int i_input;
	ifstream fin("data.out");
	ffor(i,300)
	{
	    fin>>str;
	    arr[i] = str;

	}

	createHash(hashTable,arr);

	return 0;
}
#endif
